package com.webserve.http;

import javax.activation.MimetypesFileTypeMap;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HttpServletResponse {
    private Socket socket;
    private int statusCode = 200;//状态码默认返回200
    private String statusReason = "OK";//回访结果默认返回ok
    private File contentFile;//响应返回的文件
    //响应状态头,使用方法添加响应头
    private Map<String, String> headers = new HashMap<>();
    //存储所有的数据格式
    private static MimetypesFileTypeMap mime = new MimetypesFileTypeMap();

    public HttpServletResponse(Socket socket) {
        this.socket = socket;
    }

    public void start()throws IOException{
        sendResponseLine();
        sendHeader();
        sendContent();

    }


    /**
     * 发送响应行
     */
    public void sendResponseLine() throws IOException {
        println("HTTP/1.1" + " " + statusCode + " " + statusReason);
    }

    /**
     * 发送消息头
     */
    public void sendHeader() throws IOException {
        Set<Map.Entry<String, String>> head = headers.entrySet();
        for (Map.Entry<String, String> e : head) {
            String key = e.getKey();
            String value = e.getValue();
            println(key + ": " + value);
        }
        println("");
    }

    /**
     * 发送响应正文
     */
    public void sendContent() throws IOException{
        OutputStream os = socket.getOutputStream();
        if (contentFile != null) {
            try (FileInputStream fis = new FileInputStream(contentFile);) {
                byte[] bytes = new byte[1024 * 10];
                int b;
                while ((b = fis.read(bytes)) != -1) {
                    os.write(bytes, 0, b);
                }
            }
        }
    }


    /**
     * 传入字符串,将字符串作为响应输出
     */
    public void println(String line) throws IOException {
        OutputStream os = socket.getOutputStream();
        os.write(line.getBytes(StandardCharsets.UTF_8));
        os.write(13);
        os.write(10);
    }

    /**
     * 传入需要发送的文件,同时将相关的两个消息头(文件格式,文件长度)添加到消息头散列表中
     */
    public void setContentFile(File contentFile) {
        this.contentFile = contentFile;
        addHeader("Content-type", mime.getContentType(contentFile));
        addHeader("Content-length", contentFile.length() + "");
    }


    public void sendRedirect(String location){
        setStatusCode(302);
        setStatusReason("Moved Temporarily");
        addHeader("Location",location);

    }

    public void addHeader(String name,String value){ headers.put(name,value); }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getContentFile() {
        return contentFile;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public static MimetypesFileTypeMap getMime() {
        return mime;
    }

    public static void setMime(MimetypesFileTypeMap mime) {
        HttpServletResponse.mime = mime;
    }
}
